<template>
  <div class="app-container">
    <div class="top">
      <el-tooltip
        class="item"
        effect="dark"
        content="管理用户"
        placement="top"
      >
        <el-button>用户列表</el-button>
      </el-tooltip>
    </div>
    <div class="left">
      <el-button type="primary" @click="addUser">新增用户</el-button>
    </div>

    <!-- 用户列表模块 -->
    <el-table
      :data="tableData"
      style="width: 100%"
      highlight-current-row
    >
      <el-table-column
        type="index"
        width="50"
      />
      <el-table-column
        prop="username"
        label="用户名"
        width="180"
      />
      <el-table-column
        prop="role"
        label="角色"
      />
      <el-table-column
        prop="create_time"
        label="创建时间"
      />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="edit(scope.$index, scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="remove(scope.$index, scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增用户模块 -->
    <el-dialog title="添加用户" :visible.sync="isVisible">
      <el-form :model="form">
        <el-form-item label="用户名称" label-width="120px">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="用户密码" label-width="120px">
          <el-input v-model="form.pass" type="password" />
        </el-form-item>
        <el-form-item label="用户角色" label-width="120px">
          <el-select v-model="form.role" class="origin">
            <el-option label="超级管理员" value="admin" />
            <el-option label="商家" value="trader" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: 'Usermanage',
  data() {
    return {
      tableData: [],
      isVisible: false,
      form: {
        name: '',
        pass: ''
      },
      role: ''
    }
  },

  created() {
    this.init()
  },

  methods: {
    // 进入用户列表中，显示用户的相关数据
    init() {
      this.$api.getUser({}).then(res => {
        res.data.map(e => {
          e.create_time = this.$moment.dataParse(e.create_time, 'YYYY-MM-DD')
          if (e.role === 'admin') e.role = '超级管理员'
          if (e.role === 'trader') e.role = '商家'
        })
        this.tableData = res.data
      })
    },

    edit(index, row) {
      console.log(index, row)
    },

    remove(index, row) {
      console.log(index, row)
    },

    addUser() {
      this.isVisible = true
    },

    submit() {
      // 点击添加新用户的时候，需要对数据库进行查重操作
     const option = {
       username: this.form.name
     }
    // 判断用户名是否存在
     this.$api.userExist(option).then(res => {
       if (res) {
         return this.$dialog.error('用户名已重复，请更换')
       }

      const data = {
        username: this.form.name,
        password: this.form.pass,
        role: this.form.role,
        create_time: Date.now()
      }
      // 用于添加用户
      this.$api.addUser(data).then(() => {
        this.isVisible = false
        this.init()
      })

     })
    }
  }
}
</script>

<style lang="scss" scoped>
  .app-container {
    .top {
      text-align: center;
    }
  }
</style>
